export default {
  /**9-16新增大还原方法，供组件通过mapMutations调用 */
  reductionInit: (state) => {
    state.uPlatformId = '';
    state.uObjApp = {};
    Object.assign(state.conditional, {
      isStar: false,
      unread: 0,
      bubbling: 0,
      isOnlyNoReply: false,
      fromSourceId: '',
    })
    state.conditional.selftag.splice(0, state.conditional.selftag.length)
    state.conditional.unselftag.splice(0, state.conditional.unselftag.length)
    state.conditional.isInTime = false;
    state.conditional.inTimeBegin = '';
    state.conditional.inTimeEnd = '';
  },


  setSelfTags: (state, arr) => {
    arr.forEach((v) => {
      state.selfTags.push(v)
    })
  },
  updataSelfTags: (state, { esId, uId, tag, action }) => {
    let basePTags = _.cloneDeep(state.selfTags).find((v) => {
      return v.id == tag.pid;
    })
    if (state.uObjCus.eSCompanyId == esId && state.uObjCus.id == uId) {
      let cusPTags = state.uObjCus.selfTag.find((v) => {
        return v.type == tag.pid;
      })
      if (basePTags == undefined) { return; }//如果找不到合法项（不太可能），就跳出
      if (cusPTags == undefined) { // 父级不存在则直接添加，
        state.uObjCus.selfTag.push({
          type: basePTags.id,
          typeName: basePTags.typeName,
          isMultiple: basePTags.isMultiple,
          selfTags: [{
            selfTagId: tag.value,
            tagName: tag.label
          }]
        })
      } else { // 父级存在，
        let idx = cusPTags.selfTags.findIndex((v) => {
          return v.selfTagId == tag.value;
        })
        if (idx > -1) {
          cusPTags.selfTags.splice(0, 1);
        } else {
          cusPTags.selfTags.splice(0, 1, {
            selfTagId: tag.value,
            tagName: tag.label
          });
        }
      }
    };
    /**以上是处理右侧用户信息展示用的 */
    /**以下是处理中间列表展示信息用的 */
    let cusAtMid = state.cusList_Page_state.list.find((v) => {
      return v.eSCompanyId == esId && v.id == uId
    });//中间客户列表；
    if (cusAtMid == undefined) { return };//如果不在可见列表就跳出
    let cusAtMidPTags = cusAtMid.selfTag.find((v) => {
      return v.type == tag.pid;
    })
    // return
    if (cusAtMidPTags == undefined) {
      cusAtMid.selfTag.push({
        type: basePTags.id,
        typeName: basePTags.typeName,
        isMultiple: basePTags.isMultiple,
        selfTags: [{
          selfTagId: tag.value,
          tagName: tag.label
        }]
      })
    } else {
      let idx = cusAtMidPTags.selfTags.findIndex((v) => {
        return v.selfTagId == tag.value;
      })

      if (idx > -1) {
        cusAtMidPTags.selfTags.splice(0, 1);
      } else {
        cusAtMidPTags.selfTags.splice(0, 1, {
          selfTagId: tag.value,
          tagName: tag.label
        });
      }
    }
    return;
  },
  switchUseConditionalMass: (state) => {
    state.useConditionalMass = !state.useConditionalMass;
  },
  setMobile: (state, val) => {
    state.isMobile = val;
  },
  setMobileView: (state, val) => {
    state.mobileView = val;
  },
  setLeftView: (state, val) => {
    if (val) {
      let idx = state.leftView.indexOf(val)
      if (idx > -1) {
        state.leftView.splice(idx, 1)
      } else {
        state.leftView.push(val);
      }
    } else {
      state.leftView.splice(0, state.leftView.length)
    }
    return
  },
  setMiddleView: (state, str) => {
    if (!Boolean(state.uObjApp.pid)) {
      state.uObjApp.pid = state.listApp[0].id;
    };//切换中间列表视图前检测平台选择,没有则取[0]赋值
    if (str != 'list') {
      state.conditional.unread = 0;
      state.conditional.bubbling = 0;
    }
    state.middleView = str;
  },// 客户选择区,切换散客,组织,标签用
  setFromSources: (state, arrObj) => {
    state.fromSources.splice(0, 1);
    arrObj.forEach((v) => {
      state.fromSources.push(v)
    })
  },

  setDraftView: (state, str) => {
    state.draftView = str;
  },// 草稿区,主要切换文本图片文件视频之类
  //修改不同区域视图的方法
  setDraft: (state, { type, obj }) => {
    // console.log(`收到素材类型${type},缓存中`)
    switch (type) {
      case 'clipboard':
        state.draftClipboard = obj;
        break;
      case 'text':
        state.draftText = obj;
        break;
      case 'image':
        state.draftImage = obj;
        break;
      case 'mpnews':
        state.draftMpnews = JSON.parse(JSON.stringify(obj));
        break;
      case 'video':
        state.draftVideo = obj;
        break;
      case 'file':
        state.draftFile = obj;
        break;
      default:
        return;
    }
  },
  clearDraft: (state, type) => {
    if (['all', 'clipboard'].includes(type)) {
      state.draftClipboard = '';
    }
    if (['all', 'text'].includes(type)) {
      state.draftText = '';
    }
    if (['all', 'image'].includes(type)) {
      state.draftImage = {};
    }
    if (['all', 'mpnews'].includes(type)) {
      state.draftMpnews = {};
    }
    if (['all', 'video'].includes(type)) {
      state.draftVideo = {};
    }
    if (['all', 'file'].includes(type)) {
      state.draftFile = {};
    }
  },//清空草稿区

  setListApp: (state, list) => {
    state.listApp.splice(0, state.listApp.length);
    for (let i in list) {
      let sub = list[i];
      sub.unread = sub.list.reduce(
        (t, v) => { return t + (v.isTop ? v.unread : 0) }, 0
      );//统计该平台下，全频道未读数
      sub.numHide = sub.list.reduce(
        (t, v) => { return t + !v.isHidden }, 0
      );//统计该平台下，全频道被隐藏数
      sub.isMore = false;
      state.listApp.push(sub)
    }
  },//设置左侧应用列表的动作
  fixAppUnread: (state, { pid, aid, unread }) => {
    if (pid == '') { return }
    let p = state.listApp.find((v) => { return v.id == pid })
    if (p == undefined) { return }
    let s = p.list.find((v) => { return v.id == aid })
    if (s == undefined) { return }
    if (s.unread != unread) { s.unread = unread }
  },

  tempShowAppHide: (state, { pid, sid }) => {
    // console.log(pid, sid)
    state.listApp.find((sub) => {
      return sub.id == pid
    }).list.find((v) => {
      return v.id == sid
    }).isHidden = true;
    console.log(state.listApp)
  },
  setPlatformId: (state, val) => {
    // let v = Boolean(val) ? val : state.listApp[0].id;
    state.uPlatformId = val;
    // state.uObjApp.pid = val;
  },
  setCusAppInfo: (state, { isAll, arr }) => {
    state.cusAppArray.isAll = isAll;
    let tar = state.cusAppArray;
    tar.arrId.splice(0, tar.arrId.length);
    for (let i in arr) {
      tar.arrId.push(arr[i])
    }
  },

  setCusTags: (state, list) => {
    state.uObjCusTags.splice(0, state.uObjCusTags.length)
    for (let i in list) {
      state.uObjCusTags.push(list[i])
    }
  },
  setListCus: (state, list) => {
    let arr = state.aObjCus.map((v) => { return v.id });
    state.listCus.splice(0, state.listCus.length)
    list.forEach((v) => {
      v.checked = arr.includes(v.id)
      state.listCus.push(v);
    });//同时参考已选目标组，渲染勾选
    // state.cusList_Page_state.list = list;
    let sclist = state.cusList_Page_state.list;
    let scPage = state.cusList_Page_state.page;
    let scStatus = state.cusList_Page_state.status;
    list.forEach((v) => { sclist.push(v) })
  },
  setCusList_Page_state: (state, { arr, page, status }) => {
    state.cusList_Page_state.list = [];
    let sclist = state.cusList_Page_state.list;
    let scPage = state.cusList_Page_state.page;
    let scStatus = state.cusList_Page_state.status;
    arr.forEach((v) => { sclist.push(v) })
    scPage.current = page.current;
    scPage.total = page.total;
    scStatus.total = status.total;
    scStatus.maopao = status.maopao;
    state.cusList_Page_state.isLock = false;
  },
  setCusList_soloAttr: (state, { id, key, value }) => {
    let tar = state.cusList_Page_state.list.find((v) => { return v.id == id });
    if (tar == undefined) { return }
    tar[key] = value
  },

  setTreeOrg: (state, tree) => {
    console.log(tree)
    state.treeOrg.splice(0, state.treeOrg.length);
    if (tree.length > 0) {
      state.treeOrg.push(tree[0]);
    }
  },
  setLabelList: (state, list) => {
    state.listLabel.splice(0, state.listLabel.length);
    list.forEach((v) => {
      state.listLabel.push(v)
    })
  },
  updataCusNickName: (state, opt) => {
    if (opt.cusId == state.uObjCus.id) {
      state.uObjCus.nickname = opt.newName
    };

    state.cusList_Page_state.list.forEach((v) => {
      if (v.id == opt.cusId) {
        v.nickname = opt.newName
      }
    })
  },//提供改名后全内存查值改名

  resetDialogue(state, opt) {
    state.rightView = 'none';
    if (!Boolean(opt)) {
      state.uObjCus = {};
      opt = {
        cleanMultiple: true,
        cleanPlatformId: true,
        cleanApp: true,
      }
    }
    if (Boolean(opt.cleanMultiple)) {
      state.aObjCus.splice(0, state.aObjCus.length);
      state.aTreeOrg.splice(0, state.aTreeOrg.length);
      state.aObjLabel.splice(0, state.aObjLabel.length);
    } else {
      state.uObjCus = {};
    }
    if (Boolean(opt.cleanApp)) {
      state.uObjApp = {};
    }
  },

  setObjApp: (state, value) => {
    if (value == "clear") {
      state.uObjApp = { pid: undefined, id: undefined, name: undefined, icon: undefined, }
    } else {
      state.uObjApp = Object.assign(
        { pid: undefined, id: undefined, name: undefined, icon: undefined, },
        JSON.parse(JSON.stringify(value))
      )
    }
  },//对单个应用进行覆写
  setUObjCus: (state, Obj) => {
    state.uObjCus = JSON.parse(JSON.stringify(Obj));
  },//对单个用户进行覆写


  setRightView: (state, str) => {
    let isAll = state.isAlltouser;
    let len = [].concat(
      state.aObjCus,//散客组
      state.aTreeOrg,//组织结构组
      state.aObjLabel,//标签组
    ).length;
    let s = "id" in state.uObjCus;
    let autoStr = (function () {
      if (isAll) { return 'singular'; }
      if (len > 0) { return 'plural'; }
      if (s) { return 'singular' };
      return 'none';
    })();
    state.rightView = str || autoStr;
  },// 聊天对话区,主要切换单发和群发
  setAObjCus: (state, { action, obj }) => {
    if (action == 'clear') {
      state.aObjCus.splice(0, state.aObjCus.length);
      return;
    };//清空指令优先
    let idx = state.aObjCus.findIndex((v) => { return v.id == obj.id })//检索位置备用，-1标记不存在
    if (action == "add" && idx < 0) {
      state.aObjCus.push(obj)
    }
    if (action == "remove" && idx >= 0) {
      state.aObjCus.splice(idx, 1)
    }
  },//多选用户检测指令与位置后处理,仅用于带翻页的用户列表,后两者直接覆写
  setATreeOrg: (state, { action, arr }) => {
    switch (action) {
      case 'clear'://清空指令优先
        state.aTreeOrg.splice(0, state.aTreeOrg.length);
        break;
      case 'updata':
        state.aTreeOrg.splice(0, state.aTreeOrg.length);
        arr.forEach((v) => { state.aTreeOrg.push(v) })
        break;
      case 'remove':
        let idx = state.aTreeOrg.findIndex((v) => { return v.id = arr[0] });
        state.aTreeOrg.splice(idx, 1)
        break;
      default:
        return state.aTreeOrg
    }
  },
  setAObjLabel: (state, { action, obj }) => {
    if (action == 'clear') {
      state.aObjLabel.splice(0, state.aObjLabel.length);
      return;
    };//清空指令优先
    let idx = state.aObjLabel.findIndex((v) => { return v.id == obj.id })
    if (action == "add" && idx < 0) {
      state.aObjLabel.push(obj)
    }
    if (action == "remove" && idx >= 0) {
      state.aObjLabel.splice(idx, 1)
    }
  },
  // setToUserSelectAll

  全选主职架构标签: (state, value) => {
    if (value) { state.uObjCus = {}; }//确认为@all时清空单人
    state.isAlltouser = value;
  },//设置全选,按平台使用 "@all" 作为参数发送用,受平台影响,切换平台应传false
  showCusInfo: (state, { x, y, obj }) => {
    state.cusInfo.visibility = true;
    state.cusInfo.x = x;
    state.cusInfo.y = y;
    state.cusInfo.content = obj;
  },
  closeCusInfo: (state) => {
    state.cusInfo.visibility = false;
  },


};